package com.gwcm.model.view;

import com.gwcm.model.entity.Product;
import com.gwcm.model.vo.SaleItemVo;
import com.gwcm.service.ISaleService;
import com.gwcm.service.ProductService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * 记账本
 * GRASP:控制器
 */
@Component
public class Register {
    @Resource
    private ISaleService saleService;
    @Resource
    private ProductService productService;

    /**
     * 开始一次新的销售
     */
    public void makeNewSale(){
        saleService.makeNewSale();
    }

    /**
     * 输入商品
     * @param itemSn
     * @param quantity
     * @return
     */
    public SaleItemVo enterItem(String itemSn, int quantity){
        SaleItemVo saleItemVo = null;
        Product product = productService.getProductByProductSn(itemSn);
        if(product != null){
            saleService.makeLineItem(product, quantity);
            saleItemVo = new SaleItemVo(itemSn, product.getName(), product.getPrice(), quantity);
        }
        return saleItemVo;
    }

    /**
     * 结束销售
     * @return
     */
    public BigDecimal endSale(){
        return saleService.endSale();
    }

    /**
     * 确认支付
     * @param cashTendered
     * @return
     */
    public BigDecimal makePayment(BigDecimal cashTendered){
        return saleService.makePayment(cashTendered);
    }
}
